/*
 * File     : PackagedNativeString.java
 * Created  : 22 Apr 2011
 *
 * Copyright © 2011 Matthew Wilson (mj. {my-surname} .uk {at} gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.dni.type;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;

import com.googlecode.dni.Allocator;
import com.googlecode.dni.Library;
import com.googlecode.dni.internal.DniInternal;
import com.googlecode.dni.internal.string.FastCharset;


/**
 * <p>
 *  Encapsulates a string in a form appropriate for a native call.
 * </p>
 * <p>
 *  This type makes no distinction between the various encoding options for
 *  native strings; it is simply a wrapper for an array of native characters.
 * </p>
 *
 * @author Matthew Wilson
 */
final class PackagedNativeString implements NativeString
{

    private static final Charset DEFAULT = Charset.defaultCharset();

    private final long pointer;

    private boolean disposed;


    /**
     * <p>
     *  Creates a new instance from an existing pointer.
     * </p>
     * <p>
     *  This constructor assumes that the pointer can be freed by the system
     *  {@link Allocator} as normal on a call to {@link #free()}.
     * </p>
     *
     * @param pointer
     *            the pointer
     */
    public PackagedNativeString( final Pointer pointer )
    {
        if ( pointer == null || Pointer.NULL.equals( pointer ) )
        {
            throw new NullPointerException();
        }

        this.pointer = pointer.getAddress();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Pointer pointer()
    {
        return new Pointer( this.pointer );
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isSameObject( final NativeObject other )
    {
        return other.getClass() == getClass() && ( (PackagedNativeString) other ).pointer == this.pointer;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void free()
    {
        if ( this.disposed )
        {
            throw new IllegalStateException();
        }

        this.disposed = true;
        DniInternal.ALLOCATOR.free( pointer() );
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals( final Object obj )
    {
        if ( obj == null || obj.getClass() != getClass() )
        {
            return false;
        }

        return ( (PackagedNativeString) obj ).pointer == this.pointer;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode()
    {
        return pointer().hashCode();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString()
    {
        return "NativeString[0x" + Long.toHexString( this.pointer ) + "]";
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String read( final String charset )
    {
        return FastCharset.getInstance( charset ).decode( this.pointer );
    }

}
